package com.api.oa.services.impl;

import cn.hutool.core.io.FileUtil;
import com.api.oa.mapper.FileListMapper;
import com.api.oa.mapper.OrgMapper;
import com.api.oa.model.entity.FileList;
import com.api.oa.model.entity.OaOrg;
import com.api.oa.model.vo.FileVO;
import com.api.oa.services.IFileService;
import com.api.oa.services.IOaOrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 文件管理
 */
@Service
public class FileServiceImpl implements IFileService {

    @Autowired
    private FileListMapper fileListMapper;


    @Override
    public Integer add(FileList oaOrg) {
        return fileListMapper.insert(oaOrg);
    }

    @Override
    public void remove(Long id) {
        FileList one = one(id);
        if(one == null)return;
        fileListMapper.delete(id);
        FileUtil.del(new File(one.getFilePath()));
    }

    @Override
    public Integer update(FileList org) {
        return fileListMapper.update(org);
    }

    @Override
    public PageInfo<FileVO> list(Long classify, String uid,String keyWord,Integer pageIndex,Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        PageHelper.orderBy("uploadTime desc");
        PageInfo<FileVO> pageInfo = new PageInfo<>(fileListMapper.list(classify,uid,keyWord));
        return pageInfo;
    }

    @Override
    public FileList one(Long id) {
        return fileListMapper.findOne(id);
    }
}
